package com.imooc.luckymoney.controller;

import com.imooc.luckymoney.entity.Consumer;
import com.imooc.luckymoney.entity.Luckymoney;
import com.imooc.luckymoney.entity.Result;
import com.imooc.luckymoney.entity.LuckymoneyId;
import com.imooc.luckymoney.enums.ResultEnum;
import com.imooc.luckymoney.repository.LuckymoneyRepository;
import com.imooc.luckymoney.service.LuckymoneyService;
import com.imooc.luckymoney.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/luckymoneys")
public class LuckymoneyController {

    @Autowired
    private LuckymoneyRepository repository;

    @Autowired
    private LuckymoneyService service;

    @GetMapping()
    public List<Luckymoney> list(){
        return repository.findAll();
    }

    @PostMapping("/create")
    public Result create(@Valid Luckymoney luckymoney, BindingResult bindingResult){

        return service.create(luckymoney);
    }

    @GetMapping("/find")
    public Result<Luckymoney> findById(@Valid LuckymoneyId id, BindingResult bindingResult){

        return service.findById(id);
    }

    @PutMapping("/update")
    public Result update(@Valid Consumer consumer, BindingResult bindingResult){

        return service.update(consumer);
    }

//    @PostMapping("/two")
//    public void creatTwo(){
//        service.creatTwo();
//    }

    @DeleteMapping("/delete")
    public Result deleteById(@Valid LuckymoneyId id,BindingResult bindingResult){

        return service.deleteById(id);
    }

    @GetMapping(value = "/check/money/{id}")
    public String checkMoney(@PathVariable("id") int id) throws Exception{
        return service.checkMoney(id);
    }
}
